package com.longy.gulimall.product.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.XmlUtil;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.jms.*;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 首页控制器
 *
 * @author Administrator
 * @date 2023/04/05
 */
@RestController
@RequestMapping("index")
public class TestController implements BeanPostProcessor {
    @Autowired
    private RedissonClient redisson;
    @Autowired
    private ExecutorService executorService;

    static String CN_UPPER_MONETARY_UNIT[] = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};
    static String CN_UPPER_NUMBER[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};

    public static String convertToChinese(BigDecimal amount) {
        if (amount == null) {
            return "零元整";
        }

        String numStr = amount.setScale(2, BigDecimal.ROUND_HALF_UP).toString(); // 保留两位小数
        String[] parts = numStr.split("\\.");

        String result = convertLeft(parts[0]) + convertRightPart(parts[1]);

        if (result.equals("")) {
            return "零元整";
        } else {
            return result;
        }
    }


    private static String convertRightPart(String decimalPart) {
        StringBuilder result = new StringBuilder();
        if ("00".equals(decimalPart)) {
            return result.append("整").toString();
        }
        for (int i = 0; i < decimalPart.length(); i++) {
            int digit = Integer.parseInt(String.valueOf(decimalPart.charAt(i)));
            if (digit == 0) {
                continue;
            }
            String unit = i == 0 ? "角" : "分";

            result.append(CN_UPPER_NUMBER[digit]).append(unit);
        }

        return result.toString();
    }

    private static String convertLeft(String left) {                //处理整数部分。

        int length = left.length();                                    //根据单位'亿','万','元'，把整数部分分为3种情况处理。

        if (length <= 4) {                                            //金额在千元以内。
            if (length == 1 && Integer.valueOf(left) == 0)            //金额为'0'元时的特殊情况。
                return "零元";
            return convertPart(left, length);
        } else if (length <= 8) {                                    //金额在千万元以内。

            String part1 = left.substring(0, length - 4);
            String part2 = left.substring(length - 4, length);

            String result1 = convertPart(part1, length);
            String result2 = convertPart(part2, 4);

            return result1 + result2;
        } else if (length <= 12) {                                    //金额在千亿元以内。

            String part1 = left.substring(0, length - 8);            //截取单位为'亿'部分数值。
            String part2 = left.substring(length - 8, length - 4);    //截取单位为'万'部分数值。
            String part3 = left.substring(length - 4, length);        //截取单位为'元'部分数值。

            String result1 = convertPart(part1, length);            //转换单位为'亿'部分数值。
            String result2 = convertPart(part2, 8);                    //转换单位为'万'部分数值。
            String result3 = convertPart(part3, 4);                    //转换单位为'元'部分数值。

            String result = result1 + result2 + result3;
            return result;
        } else {
            throw new IllegalArgumentException("超出转换数值范围！");
        }

    }

    private static String convertRight(String right) {                //处理小数部分。

        String result = "";
        String number = "";
        String unit = "";

        int length = right.length();

        for (int i = 0; i < length; i++) {
            String detail = right.substring(i, i + 1);
            int value = Integer.valueOf(detail);
            number = CN_UPPER_NUMBER[value];
            unit = CN_UPPER_NUMBER[i];
            result = result.concat(number).concat(unit);
        }

        result = result.replaceAll("零角", "").replaceAll("零分", "").replaceAll("零厘", "");
        return result;
    }

    private static String convertPart(String part, int position) {

        String result = "";
        String number = "";
        String unit = "";

        if (part.equals("0000") && position == 8)                            //用于排除单位为'万'时，四位数值均为'0'的情况。
            return "";
        for (int i = 0; i < part.length(); i++) {
            int value = Integer.valueOf(String.valueOf(part.charAt(i)));
            number = CN_UPPER_NUMBER[value];
            unit = CN_UPPER_MONETARY_UNIT[position - 1 - i];
            result = result.concat(number).concat(unit);
        }

        result = result.replaceAll("零仟", "零").replaceAll("零佰", "零")        //把单位'亿','万','元'放到最后替换。
                .replaceAll("零拾", "零")
                .replaceAll("零零", "零").replaceAll("零零", "零").replaceAll("零亿", "亿")
                .replaceAll("零万", "万").replaceAll("零元", "元");

        System.out.println(result);
        return result;
    }

    public static void main(String[] args) throws JMSException {
        Date date = new Date();
        DateTime dateTime = DateUtil.offsetMinute(date, 2);
        Date date2 = new Date();
        int compare = DateUtil.compare(dateTime, date2);

        System.out.println(compare > 0);

    }
}
